﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Terraria.DataStructures;
using Terraria.World.Generation;

namespace Terraria.GameContent.Generation
{
	// Token: 0x02000437 RID: 1079
	public class ShapeFloodFill : GenShape
	{
		// Token: 0x06002531 RID: 9521 RVA: 0x0001A30C File Offset: 0x0001850C
		public ShapeFloodFill(int maximumActions = 100)
		{
			this._maximumActions = maximumActions;
		}

		// Token: 0x06002532 RID: 9522 RVA: 0x00484328 File Offset: 0x00482528
		public override bool Perform(Point origin, GenAction action)
		{
			Queue<Point> queue = new Queue<Point>();
			HashSet<Point16> hashSet = new HashSet<Point16>();
			queue.Enqueue(origin);
			int num = this._maximumActions;
			while (queue.Count > 0)
			{
				if (num <= 0)
				{
					IL_154:
					while (queue.Count > 0)
					{
						Point point = queue.Dequeue();
						if (!hashSet.Contains(new Point16(point.X, point.Y)))
						{
							queue.Enqueue(point);
							break;
						}
					}
					return queue.Count == 0;
				}
				Point point2 = queue.Dequeue();
				if (!hashSet.Contains(new Point16(point2.X, point2.Y)))
				{
					bool flag = base.UnitApply(action, origin, point2.X, point2.Y, new object[0]);
					if (flag)
					{
						hashSet.Add(new Point16(point2));
						num--;
						if (point2.X + 1 < Main.maxTilesX - 1)
						{
							queue.Enqueue(new Point(point2.X + 1, point2.Y));
						}
						if (point2.X - 1 >= 1)
						{
							queue.Enqueue(new Point(point2.X - 1, point2.Y));
						}
						if (point2.Y + 1 < Main.maxTilesY - 1)
						{
							queue.Enqueue(new Point(point2.X, point2.Y + 1));
						}
						if (point2.Y - 1 >= 1)
						{
							queue.Enqueue(new Point(point2.X, point2.Y - 1));
						}
					}
				}
			}
			goto IL_154;
		}

		// Token: 0x0400411C RID: 16668
		private int _maximumActions;
	}
}
